package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.RoastingDust;
import com.hkts.naturalgas.base.entity.RoastingPurificationBleaching;
import com.hkts.naturalgas.base.entity.RoastingPurifyWetElectricity;
import com.hkts.naturalgas.base.mapper.RoastingPurificationBleachingMapper;
import com.hkts.naturalgas.base.service.IRoastingPurificationBleachingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class RoastingPurificationBleachingServiceImpl extends ServiceImpl<RoastingPurificationBleachingMapper, RoastingPurificationBleaching> implements IRoastingPurificationBleachingService {


    @Autowired
    private RoastingPurificationBleachingMapper roastingPurificationBleachingMapper;

    @Override
    public IPage<RoastingPurificationBleaching> selectPage(Page<RoastingPurificationBleaching> page, RoastingPurificationBleaching roastingPurificationBleaching){
            QueryWrapper<RoastingPurificationBleaching> query=Wrappers.query();
            QueryWrapper<RoastingPurificationBleaching> entityWrapper=getEntityWrapper(query, roastingPurificationBleaching);
            return roastingPurificationBleachingMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<RoastingPurificationBleaching> selectList(RoastingPurificationBleaching roastingPurificationBleaching){
            QueryWrapper<RoastingPurificationBleaching> query=Wrappers.query();
            QueryWrapper<RoastingPurificationBleaching> entityWrapper=getEntityWrapper(query, roastingPurificationBleaching);
            return roastingPurificationBleachingMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingPurificationBleaching> getEntityWrapper(QueryWrapper<RoastingPurificationBleaching> entityWrapper,RoastingPurificationBleaching roastingPurificationBleaching){

            return entityWrapper;
    }

    @Override
    public IPage<RoastingPurificationBleaching> selectPageByTime(Page<RoastingPurificationBleaching> page, String startDate, String endDate){
        QueryWrapper<RoastingPurificationBleaching> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingPurificationBleaching::getTime);
        IPage<RoastingPurificationBleaching> hktsTailGasPage = roastingPurificationBleachingMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingPurificationBleaching> getBrokenLineGraph(Page<RoastingPurificationBleaching> page,String startDate, String endDate){
        QueryWrapper<RoastingPurificationBleaching> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingPurificationBleaching::getTime);
        IPage calcinationDesulfurizationDenitrations = roastingPurificationBleachingMapper.selectPage(page,query);
        List<RoastingPurificationBleaching> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingPurificationBleaching vo){
        Map<String,Object> data = new LinkedHashMap<>();
        /*data.put("焙烧A风机电流(A)",vo.getBsjhFan1dl());*/
        data.put("焙烧A风机频率(Hz)",vo.getBsjhFan1sv());
        /*data.put("焙烧B风机电流(A)",vo.getBsjhFan2dl());*/
        data.put("焙烧B风机频率(Hz)",vo.getBsjhFan2sv());
        /*data.put("焙烧脱白塔进口湿度(%)",vo.getBsjhSidu());
        data.put("焙烧脱白塔出口湿度(%)",vo.getBsjhSidu2());
        data.put("焙烧脱白塔进口温度(℃)",vo.getBsjhTe102());
        data.put("焙烧脱白塔出口温度(℃)",vo.getBsjhTe105());*/
        data.put("time",vo.getTime());
        return data;
    }
    @Override
    public void deleteAll(List<String> collect) {
        roastingPurificationBleachingMapper.deleteAllData();
    }

    @Override
    public void dataHndler519() {
        QueryWrapper<RoastingPurificationBleaching> query=Wrappers.query();
        query.le(RoastingPurificationBleaching.TIME,"2021-05-18 23:59:56");
        List<RoastingPurificationBleaching> roastingPurificationBleachings = roastingPurificationBleachingMapper.selectList(query);
        Float bsjhFan1sv;
        /**
         * 焙烧B风机电流
         */
        Float bsjhFan2dl;
        for(int i=0;i<roastingPurificationBleachings.size();i++){
            bsjhFan1sv= roastingPurificationBleachings.get(i).getBsjhFan1sv();
            bsjhFan2dl=roastingPurificationBleachings.get(i).getBsjhFan2dl();
            roastingPurificationBleachings.get(i).setBsjhFan1sv(bsjhFan2dl);
            roastingPurificationBleachings.get(i).setBsjhFan2dl(bsjhFan1sv);
        }
        this.saveOrUpdateBatch(roastingPurificationBleachings);
    }
}
